import var, random
from control.setting_control import new_process
from gui.ui_about import CreateAboutWindow
from gui.ui_setting import CreateSettingWindow, SettingParams

from var import core, screen, author
from workThread.FIFOWorker import FIFOWorker
from workThread.LRUWorker import LRUWorker


def new_click(ui_main):
    core.reset()
    for i in range(var.rand_num):
        core.addTime(random.randint(0, 255))
    ui_main.refreshPages(core.getTimes())


def about_click():
    print(author)
    aboutWin = CreateAboutWindow(screen)
    aboutWin.show()
    aboutWin.exec()


def setting_click(ui_main):
    new_params = SettingParams(new_process)
    newWin = CreateSettingWindow(screen, new_params, ui_main)
    newWin.show()
    newWin.exec()


def reset_click(ui_main):
    core.reset()
    ui_main.page_show.setText("当前请求下标：未开始")
    ui_main.refreshPages(core.getTimes())
    ui_main.refreshOutput("已重置")


def run_LRU(ui_main):
    print("receive run_click")
    global ui_worker
    ui_worker = LRUWorker(core.getTimes(), var.frames_num)
    ui_worker.current_index_signal.connect(ui_main.refreshIndex)
    ui_worker.current_pages_signal.connect(ui_main.refreshCurrent)
    ui_worker.output_signal.connect(ui_main.refreshOutput)
    ui_worker.start()


def run_FIFO(ui_main):
    print("receive run_click")
    global ui_worker
    ui_worker = FIFOWorker(core.getTimes(), var.frames_num)
    ui_worker.current_index_signal.connect(ui_main.refreshIndex)
    ui_worker.current_pages_signal.connect(ui_main.refreshCurrent)
    ui_worker.output_signal.connect(ui_main.refreshOutput)
    ui_worker.start()
